package online.longzipeng.back.intercept;


import cn.hutool.core.util.StrUtil;
import online.longzipeng.back.common.enums.ResponseEnum;
import online.longzipeng.back.common.exception.MessageException;
import online.longzipeng.back.utils.JWTUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author lzp
 * @date 2021/4/25 13:28
 * 访客访问信息拦截
 */
public class AuthInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 校验token
        String token = request.getHeader("token");
        if (StrUtil.isBlank(token)) {
            throw new MessageException(ResponseEnum.TOKEN_INVALID);
        }
        JWTUtils.checkJwtUserInfo(token);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
